<?php

class ShowController extends Controller
{
    public function actionIndex()
    {
        $jobtype = Jobtype::model()->findAll();
        $budget = Budget::model()->findAll();
        $dataProvider=new CActiveDataProvider('Job',array(
            'pagination'=>array(
                'pageSize'=>10,
            ),
        ));
        $this->render('index',array(
            'dataProvider'=>$dataProvider,
            'jobtype'=>$jobtype,
            'budget'=> $budget,
        ));

    }

    public function actionJob($type)
    {
        $jobtype = Jobtype::model()->findAll();
        $budget = Budget::model()->findAll();
        $userid = Yii::app()->user->id;
        $criteria=new CDbCriteria;
        $criteria->join="INNER JOIN tbl_bidjob b ON t.id = b.job_id";
        $criteria->condition ="b.user_id = $userid AND t.status = $type";
        $dataProvider=new CActiveDataProvider('Job',array(
            'pagination'=>array(
                'pageSize'=>10,
            ),
            'criteria'=>$criteria,
        ));
        $this->render('index',array(
            'dataProvider'=>$dataProvider,
            'jobtype'=>$jobtype,
            'budget'=> $budget,
        ));        
    }

    public function actionAll($id)
    {
        $jobtype = Jobtype::model()->findAll();
        $budget = Budget::model()->findAll();
        $model = Job::model()->findByPk($id);
        $criteria=new CDbCriteria;
        $criteria->condition = "subjobtype_id = ".$model->subjobtype_id;
        $criteria->join = "INNER JOIN tbl_job_skill b ON t.id = b.id";
        foreach ($model->jobSkills as $key) {
            $skill_id = $key->skill_id;
            $criteria->addCondition("b.skill_id = $skill_id");
            // echo $key->id;
        }
        $dataProvider=new CActiveDataProvider('Job',array(
            'pagination'=>array(
                'pageSize'=>10,
            ),
            'criteria'=>$criteria
        ));
        $this->render('index',array(
            'dataProvider'=>$dataProvider,
            'jobtype'=>$jobtype,
            'budget'=> $budget,
        ));

    }

    public function actionSearch()
    {
        if(isset($_POST["input-search"])){
            $search = $_POST["input-search"];
            $criteria=new CDbCriteria;
            $criteria->addSearchCondition('name', $search);
            $dataProvider=new CActiveDataProvider('Job',array(
                        'criteria' => $criteria,
                        'pagination'=>array(
                              'pageSize'=>1,
                        ),
                ));
            $this->renderPartial('search', array('model'=>$dataProvider));
        }
    }
    
    public function actionAA()
    {
        $data = BidJob::model()->findByPk(9);
        $title = "Hợp đồng đã của dự án đã  ".$data->job->name." hoàn thành";
        $linkuser =Yii::app()->request->hostInfo.Yii::app()->createUrl('user/userprofiles/view',array('id'=>$data->job->user->userProfile->id ));//user/userprofiles/view
        $link =  Yii::app()->request->hostInfo.Yii::app()->controller->createUrl('/job/bid/accepted',array('id'=>$id,'ra'=>md5($data->job->name) ));
        $content ="Dear".$data->user->name." </br> Hợp đồng giữa hai bên chính thức có hiệu lực. Chúc các bạn có dự án thành công.</br>
        <a href='$linkuser'>Liên hệ với chủ dự án</a>";
        $this->render('aa');
        // Yii::app()->getModule('job')->sendMail($title,$content,$data->user->email);

    //     $cron = new Crontab('my_crontab'); // my_crontab file will store all added jobs
 
    // $jobs_obj = $cron->getJobs(); // previous jobs saved in my_crontab
 
    // foreach($jobs_obj as $job)
    //     echo $job->getCommand();
 
    // $cron->eraseJobs(); // erase all previous jobs in my_crontab
 
    // // Application console job
    // $cron->addApplicationJob('test', 'test1', array(), '10','*');
 
 
    // // <= adds a third job with: 0 0 * * 1 /home/user/myprogram.bin 
 
    // $cron->removeJob(2); // removes job with offset 2 (last added here)
 
    // $cron->saveCronFile(); // save to my_crontab cronfile
 
    // $cron->saveToCrontab(); // adds all my_crontab jobs to system (replacing previous my_crontab jobs)
    }
    public function actionType($j,$b)
    {
        if ($j == 0){
            $jobtype = Jobtype::model()->findAll();
        }else{
            $jobtype = Jobtype::model()->findAll("id= $j");
        }
        if ($b == 0){
            $budget = Budget::model()->findAll();
        }else{
            $budget = Budget::model()->findAll("id =$b");    
        }
        if ($j != 0 && $b != 0 ){
            $criteria = new CDbCriteria();
            $criteria->join ="inner join tbl_sub_jobtype sub on subjobtype_id = sub.id where sub.jobtype_id = $j and budget_id = $b";
        }elseif ($j != 0){
            $criteria = new CDbCriteria();
            $criteria->join ="inner join tbl_sub_jobtype sub on subjobtype_id = sub.id where sub.jobtype_id = $j";
        }elseif ($b != 0 ) {
            $criteria = new CDbCriteria();
            $criteria->condition ="budget_id = $b";

        }
        
        $dataProvider=new CActiveDataProvider('Job',array(
            'pagination'=>array(
                'pageSize'=>3,
            ),
            'criteria'=>$criteria,
        ));
        $this->render('type',array(
            'dataProvider'=>$dataProvider,
            'jobtype'=>$jobtype,
            'budget'=> $budget,
            'j' => $j,
            'b' => $b,
        ));        
    } 
}
?>